package com.springboot.blog.service;

import com.springboot.blog.beans.BlogShop;
import com.springboot.blog.beans.SignIn;
import com.springboot.blog.config.utils.R;
import com.springboot.blog.dao.ShopVipDao;
import com.springboot.blog.dao.SignInMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
public class VipShopServiceImpl implements VipShopService {


    @Resource
    private SignInMapper signInMapper;
    @Resource
    private ShopVipDao shopVipDao;

    @Transactional
    public R buyVipshop(String username,Integer shopId){
        SignIn shopreward = signInMapper.siginCount(username);
        BlogShop blogShop = shopVipDao.selectById(shopId);
        if (shopreward.getReward()<=0||shopreward.getReward() < blogShop.getShopPrice()){
            return R.error().data("balance","余额不足，请充值！");
        }
        int buyshop = signInMapper.buyshop(blogShop.getShopPrice());
        String shopName = blogShop.getShopName();
        if (buyshop>0){
            signInMapper.updateSigin(username,shopName);
            return R.ok();
        }
        return R.error();
    }
}
